#include "basic.h"
#include "func.h"
#include "cli_func.h"
#include<cstdio>
#include<cstring>
#include<iostream>

bool note(char letter[]); 
int sentence_type(char letter[]);
int Judge_function_type(char letter[]);
int grammar(char letter[], int line);
bool brackets(char letter[]); 
bool endwith_yr(char letter[], int line);
int sentence_type(char letter[]);
bool pd_operation(char letter[]);
bool pd_define_vars(char letter[]);
void define__del_endbeg(char letter[]);
void define_del_mid(char letter[]);
//判断注释

bool note(char letter[]) {
    if (letter[0] == '/' && letter[1] == '/')return 1;
    else return 0;
}
//判断语句类型
int Judge_function_type(char letter[]) {
    int i1 = 0, i2 = 0;//i1判断括号
    
    
    if(pd_operation(letter))return 4;
    if(pd_define_vars(letter))return 5;
    if (!std::strcmp(letter,"exit()")){
        exit(0);
    }
    if (!std::strcmp(letter,"piano()")){
        play_piano();return 0;
    }
    if (!std::strcmp(letter,"randphoto()")){
        make_random_photos();
        return 0;
    }
    if (letter[0]=='y'&&letter[1]=='f')return 10;

    while (letter[i1] != '(')++i1;//找到位置

    if (i1 == std::strlen(letter))return 1;//判断是否有

    else {
        char message[1000];
        for (int i = 0; i <= std::strlen(letter); i++)message[i] = 0;
        for (int j = 0; j < i1; j++) {//将字符串提取
            message[j] = letter[j];
        }
        // std::cout<<message<<std::endl;
        
        if (!std::strcmp(message, "yrsays"))return 2;
        if (!std::strcmp(message, "yreat"))return 3;

        if (letter[0]=='y'&&letter[1]=='f')return 10;
        return 1;
    }
    return 0;
}
//判断语法错误是否存在
int grammar(char letter[], int line) {
    int strl = std::strlen(letter);
    int i1 = 0, i2 = 0;//i1判断括号
    while (letter[i1] != '(')++i1;//找到位置
    char message[1000];
    for (int i = 0; i <= std::strlen(letter); i++)message[i] = 0;
    for (int j = 0; j < i1; j++) {//将字符串提取
        message[j] = letter[j];
    }
    if (endwith_yr(letter, line))return 1;
    return 0;
}

//括号匹配
bool brackets(char letter[]) {
    return 1;
}
//是否以" yr"结尾
bool endwith_yr(char letter[], int line) {
    int strl = std::strlen(letter);
    if ((!(letter[strl - 1] == 'r' && letter[strl - 2] == 'y' && letter[strl - 3] == ' ')) && (sentence_type(letter) == 1)) {
        std::cout << "In line "<< line<<" Should end with yr\n";
        return 1;
    }
    else {
        return 0;
    }
}

//判断语句类型
int sentence_type(char letter[]) {
    int i1 = 0, i2 = 0;//i1判断括号
    while (letter[i1] != '(')++i1;//找到位置
    char message[1000];
    for (int i = 0; i <= std::strlen(letter); i++)message[i] = 0;
    for (int j = 0; j < i1; j++) {//将字符串提取
        message[j] = letter[j];
    }
    if (!strcmp(message, "yfor"))return 10;
    if (!strcmp(message, "yile"))return 11;
    if (!strcmp(message, "yif"))return 12;
    if (!strcmp(message, "ylse"))return 13;
    return 1;
}

bool pd_operation(char letter[]){
    int symbol_times=0,symbol_position,equal_times=0,equal_position;
    for(int i=0;i<std::strlen(letter);i++){
        if(letter[i]=='+'||letter[i]=='-'||letter[i]=='*'||letter[i]=='/'||letter[i]=='^'||letter[i]=='%'){
            symbol_position=i;
            symbol_times++;
        }
        if(letter[i]=='='){
            equal_position=i;
            equal_times++;
        }
    }

    int l1=0,l2=equal_position+1,l3=symbol_position+1,r1=equal_position-1,r2=symbol_position-1,r3=strlen(letter)-4;
    // cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<" "<<l3<<" "<<r3<<endl;
    if(symbol_times==1&&equal_times==1){
        if(l1<=r1&&l2<=r2&&l3<=r3)return 1;
    }
    return 0;
}

bool pd_define_vars(char letter[]){
    int p1=0,p2;
    while(p1<=10){
        if(letter[p1]!=' ')break;
        p1++;
    }
    p2=p1;
    while(p2<=10){
        if(letter[p2]==' ')break;
        p2++;
    }
    char var[10];
    _strcpy(var,letter,p1,p2-1);
    std::string tmp=var;
    if(tmp=="yri"||tmp=="yrf"||tmp=="yrc"||tmp=="yrs"||tmp=="yrb"){
        // cout<<tmp<<endl;
        return 1;
    }
    else return 0;
}

void define__del_endbeg(char letter[]){
    int l=0,r=strlen(letter)-1;
    while(letter[l]==' ')l++;
    while(letter[r]==' ')r--;
    _strcpy(letter,letter,l,r);
    return;
}

void define_del_mid(char letter[]){
    int t=0,l=0,r=0;std::string out;
    for(int i=0;i<strlen(letter);i++){
        if(letter[i]!=' ') out+=letter[i];
        if(letter[i]==' '){
            out+=' ';
            while(letter[i+1]==' ')i++;
        }
    }
    strcpy(letter,out.c_str());
}